/**
 * Edit Page
 */
var OrderTransportInfoDlg = {
    data: {
    	setUnitId: "",
        setUnit: "",
        setLocation: "",
        setPeople: "",
        setTel: "",
        setMentiontime: "",
        setWarehousepeople: "",
        setWarehousetel: "",
        setAddress: "",
        getUnitId: "",
        getUnit: "",
        getLocation: "",
        getPeople: "",
        getTel: "",
        getMentiontime: "",
        getWarehousepeople: "",
        getWarehousetel: "",
        getAddress: "",
    }
};
/**
 * 添加或者修改页面
 */
var AddressInfoDlg = {
    data: {
    	setLocation: "",
    	setLocationId: "",
    	setPeople: "",
    	setTel: "",
    }
};

var submit = 0
layui.use(['form', 'admin', 'ax','laydate'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var laydate = layui.laydate;

    // Adjust iframe height to fit
    admin.iframeAuto();

    //执行一个laydate实例
    laydate.render({
	  type:'datetime',
      elem: '#undertime' //指定元素
    });

    //执行一个laydate实例
    laydate.render({
      elem: '#setMentiontime' //指定元素
    });

    //执行一个laydate实例
    laydate.render({
      elem: '#getMentiontime' //指定元素
    });

    // Get detail information to fill the form
    var ajax = new $ax(Feng.ctxPath + "/orderTransport/detail?id=" + Feng.getUrlParam("id"));
    var result = ajax.start();
    form.val('orderTransportForm', result.data);

    // Form submission event
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/orderTransport/editItem", function (data) {
            Feng.success("Update successful!");
            window.location.href = Feng.ctxPath + '/orderTransport'
        }, function (data) {
            Feng.error("Update failed! " + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        return false;
    });
    
    // Click loading unit
    $("#setUnit").click(function(){
		submit = 0
        layer.open({
            type: 2,
            title: 'Select Loading Unit',
            area: ['900px', '500px'],
            content: Feng.ctxPath + '/orderTransport/toChoiceUnit?putgoodsCustomerId='+$("#customerId").val(),
            end: function () {
            	if(submit == 1){
	                $("#setUnitId").val(OrderTransportInfoDlg.data.setUnitId);//Loading unit ID
	                $("#setUnit").val(OrderTransportInfoDlg.data.setUnit);//Loading unit
	                $("#setLocation").val(OrderTransportInfoDlg.data.setLocation);
	                $("#setLocationId").val(OrderTransportInfoDlg.data.setLocationId);
	                $("#setPeople").val(OrderTransportInfoDlg.data.setPeople);
	                $("#setTel").val(OrderTransportInfoDlg.data.setTel);
            	}
            }
        });
    });
    
    // Click unloading unit
    $("#getUnit").click(function(){
		submit = 0
        layer.open({
            type: 2,
            title: 'Select Unloading Unit',
            area: ['900px', '500px'],
            content: Feng.ctxPath + '/orderTransport/toChoiceUnit?putgoodsCustomerId='+$("#customerId").val(),
            end: function () {
            	if(submit == 1){
	                $("#getUnitId").val(OrderTransportInfoDlg.data.setUnitId);//Unloading unit ID
	                $("#getUnit").val(OrderTransportInfoDlg.data.setUnit);//Unloading unit
	                $("#getLocation").val(OrderTransportInfoDlg.data.setLocation);
	                $("#getLocationId").val(OrderTransportInfoDlg.data.setLocationId);
	                $("#getPeople").val(OrderTransportInfoDlg.data.setPeople);
	                $("#getTel").val(OrderTransportInfoDlg.data.setTel);
            	}
            }
        });
    });
    
    // Click loading address
    $("#setLocation").click(function(){
    	var setUnit = $("#setUnit").val();
    	if (setUnit==null||setUnit=='') {
			layer.msg('Please select loading unit'); 
		} else {
			submit = 0
            layer.open({
                type: 2,
                title: 'Select Loading Address',
                area: ['900px', '500px'],
                content: Feng.ctxPath + '/orderTransport/toAddress?putgoodsId=' + $("#setUnitId").val(),
                end: function () {
            		if(submit == 1){
	                    $("#setLocation").val(AddressInfoDlg.data.setLocation);
	                    $("#setLocationId").val(AddressInfoDlg.data.setLocationId);
	                    $("#setPeople").val(AddressInfoDlg.data.setPeople);
	                    $("#setTel").val(AddressInfoDlg.data.setTel);
            		}
                }
            });
		}
    });
    
    // Click unloading address
    $("#getLocation").click(function(){
    	var getUnit = $("#getUnit").val();
    	if (getUnit==null||getUnit=='') {
			layer.msg('Please select unloading unit'); 
		} else {
			submit = 0
            layer.open({
                type: 2,
                title: 'Select Unloading Address',
                area: ['900px', '500px'],
                content: Feng.ctxPath + '/orderTransport/toAddress?putgoodsId=' + $("#getUnitId").val(),
                end: function () {
            		if(submit == 1){
	                    $("#getLocation").val(AddressInfoDlg.data.setLocation);
	                    $("#getLocationId").val(AddressInfoDlg.data.setLocationId);
	                    $("#getPeople").val(AddressInfoDlg.data.setPeople);
	                    $("#getTel").val(AddressInfoDlg.data.setTel);
            		}
                }
            });
		}
    });

    // Click to select location
    $("#choicep").click(function(){
        var getLocation = $("#getLocation").val();
        if (getLocation==null||getLocation=='') {
            layer.msg('Please select unloading address');
        } else {
			submit = 0
            layer.open({
                type: 2,
                title: 'Edit Unloading Address',
                area: ['1000px', '800px'],
                content: Feng.ctxPath + '/outOrder/editAddress?id=' + $("#getLocationId").val()+"&setLocationId="+$("#setLocationId").val(),
                end: function () {
            		if(submit == 1){
	                    $("#getLocation").val(AddressInfoDlg.data.setLocation);
					}
                }
            });
        }
    });

    // Back button
    $("#backupPage").click(function () {
        window.location.href = Feng.ctxPath + '/orderTransport'
    });

});